#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <ctemplate/template.h>

#include "oj_model_mysql.hpp"
#include "../comm/log.hpp"
#include "../comm/util.hpp"

namespace ns_view
{
    using namespace ns_model;
    using namespace ns_log;
    using namespace ns_util;

    const std::string html_comm_path = "./ctemplate_html/";

    class View
    {
    public:
        View() = default;
        ~View() = default;
        // 渲染题目列表：key值 -- number，name，difficulty
        void ExpendAllQuestion(const std::vector<Question>& questions, std::string* html)
        {
            // 1. 拼接源路径
            std::string src_path = html_comm_path + "allquestions.html";
            // 2. 创建字典
            ctemplate::TemplateDictionary dict("allquestions");
            // 3. 循环插入value值
            for(const auto& q : questions)
            {
                //  创建分字典
                ctemplate::TemplateDictionary* sec = dict.AddSectionDictionary("question_list");
                sec->SetValue("number", q.number);
                sec->SetValue("name", q.name);
                sec->SetValue("difficulty", q.difficulty);
            }
            // 4. 获取被渲染的HTML
            ctemplate::Template* tpl = ctemplate::Template::GetTemplate(src_path, ctemplate::DO_NOT_STRIP/*不去除空行，保留原始网页数据*/);
            // 5. 开始渲染
            tpl->Expand(html, &dict);
        }
        // number name difficulty (cpu_limit, mem_limit) desc header
        void ExpendOneQuestion(const Question& q, std::string* html)
        {
            // 1. 获取源HTML的地址
            std::string src_path = html_comm_path + "onequestion.html";
            // 2. 创建字典
            ctemplate::TemplateDictionary dict("question");
            // 3. 插入value
            dict.SetValue("number", q.number);
            dict.SetValue("name", q.name);
            dict.SetValue("difficulty", q.difficulty);
            dict.SetValue("desc", q.desc);
            dict.SetValue("precode", q.header);
            // 4. 获取要被渲染的html
            ctemplate::Template* tpl = ctemplate::Template::GetTemplate(src_path, ctemplate::DO_NOT_STRIP);
            // 5. 渲染
            tpl->Expand(html, &dict);
        }
    };
}